<!doctype html>
<html>

<head>
  <meta charset="UTF-8">
  <title>工作状态-RPA机器人工作台</title>
  <meta name="renderer" content="webkit|ie-comp|ie-stand">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport"
    content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
  <meta http-equiv="Cache-Control" content="no-siteapp" />
  <link rel="stylesheet" href="/rpa_work/static/css/xadmin.css?v=1.01">
  <script type="text/javascript" src="/rpa_work/static/js/jquery.min.js"></script>
  <script type="text/javascript" src="/rpa_work/static/js/jquery.cookie.js"></script>
  <script src="/rpa_work/static/layui/layui.js?v=2.6.8" charset="utf-8"></script>
  <script>var page = "workstate";</script>
  <script type="text/javascript" src="/rpa_work/static/js/xadmin.js?v=1.0"></script>
  <script type="text/javascript" charset="utf-8" src="/rpa_work/static/js/xm-select.js"></script>
  <script type="text/javascript" src="/rpa_work/static/js/target_window.js"></script>
</head>
<body>
  <div class="refreshdata layui-hide"><span id="timeout">3</span>秒后刷新数据 </div>
  <!--        <div class="x-nav">
            <span class="layui-breadcrumb">
              <a><cite>机器人</cite></a>
              <a><cite>工作状态</cite></a>
              
            </span>
            <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"  href="javascript:location.replace(location.href);" title="刷新"><i class="layui-icon layui-icon-refresh"></i></a>
        </div>-->
  <div class="x-body">

    <div class="layui-card">
      <div class="layui-card-body">
        <div class="layui-row">
          <form class="layui-form layui-col-md12 x-so layui-form-pane" method="get" id="myform" lay-filter="searchform">
            <div class="form-item layui-inline">
              <label class="layui-form-label">机器人名称</label>
              <div class="layui-input-block">
                <select name="robot_id" id="robotchange" lay-filter="robotchange">
                  <option value="">请选择</option>
                </select>
              </div>
            </div>
            <div class="form-item layui-inline">
              <label class="layui-form-label">工作状态</label>
              <div class="layui-input-block">
                <select name="status">
                  <option value=""></option>
                  <option value="空闲中">空闲中</option>
                  <option value="工作中">工作中</option>
                </select>
              </div>
            </div>
            <div class="form-item layui-inline">
              <div class="layui-input-inline">
                <a class="layui-btn" lay-submit="" lay-filter="search" id="searchbtn">查询</a>
              </div>
            </div>
            <div class="form-item layui-inline layui-hide">
              <div class="layui-input-inline">
                <input type="checkbox" name="" id="refreshdatabtn" lay-skin="switch" lay-filter="refreshdata" lay-text="停止刷新|自动刷新" checked>
              </div>
            </div>
          </form>

        </div>

        <table class="layui-hide" lay-data="{id: 'list_table'}" id="list_table" lay-filter="list_table"></table>

      </div>
    </div>
  </div>

  <script>


    layui.use(['form', 'element', 'laypage', 'layer', 'table'], function () {
      $ = layui.jquery;//jquery
      var layer = layui.layer;//弹出层
      var form = layui.form;
      var table = layui.table;
      var refreshdata;
      var refreshtime = 5;
      $.ajax({
        url: apiurl + "WorkingCondition",
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify({ 'sid': sid }),
        headers: {
          'Content-Type': 'application/json'
        },
        success: function (res) {
          if (res.status == 0) {
            $('#robotchange').empty();
            $('#robotchange').append('<option value="">请选择</option>');
            $.each(res.data, function (index, robot) {
              var $option = $('<option></option>');
              $option.val(robot.robot_id);
              $option.text(robot.name);
              $('#robotchange').append($option);
            });
            form.render('select', 'searchform');
          } else {
            //layer.msg(res.msg);
            layer.confirm('获取不到任务名称', {
              btn: ['返回列表', '重试']
              , closeBtn: 0
            }, function () {
              parent.location.reload();
            }, function () {
              window.location.href = location.href;
            });
          }
        },
        error: function (xhr, status, error) {
          layer.msg(error);
        }
      });

      table.render({
        elem: '#list_table'
        , height: 600
        , cellMinWidth: 60
        , url: apiurl + 'WorkingCondition'
        , method: 'post'
        , contentType: 'application/json'
        , where: { 'sid': sid }
        , response: {
          statusName: 'status'
        }
        , page: true
        //,size:'lg'
        , page: {
          layout: ['count', 'prev', 'page', 'next', 'refresh']
          , groups: 5
          , first: true
          , last: true
          , limit: 100
          , first: '首页'
          , last: '尾页'
        }
        , cols: [[ //表头
          { field: 'name', title: '机器人名称', align: 'center', minWidth: 150 },
          { field: 'status', title: '工作状态', align: 'center', minWidth: 80 },
          { field: 'health', title: '健康状态', align: 'center', minWidth: 80 },
          { field: 'remark', title: '备注', align: 'center', edit: 'text', minWidth: 150 }
        ]]
        , done: function (res, curr, count) {
          checklogin(res);
          /*$('#robotchange').empty();
          $('#robotchange').append('<option value="">请选择</option>');
          $.each(res.data, function(index, robot) {
            $('#robotchange').append(new Option(robot.name, robot.robot_id));
          });
          form.render('select', 'searchform');*/

            $('#refreshdatabtn').prop('checked',true);
            form.render('checkbox', 'searchform');
          //定时3s刷新一次表格
          if (refreshdata != null) {
            clearInterval(refreshdata);
          }
          let countdown;
          countdown = refreshtime;
          $('#timeout').html(countdown);

          refreshdata = setInterval(function () {
            //console.log(countdown);
            countdown--;
            $('#timeout').html(countdown);
            if (countdown == 0) {
              $('#searchbtn').click();
              clearInterval(refreshdata);
            }
          }, 1000);
        }
      });
      form.on('switch(refreshdata)', function (data) {
        if (data.elem.checked) {
          $('#searchbtn').click();
        } else {
          clearInterval(refreshdata);
        }
      });


      table.on('edit(list_table)', function (obj) {
        let id = obj.data.robot_id;
        let value = obj.value;
        let field = obj.field;
        let postdata = { 'sid': sid, 'robot_id': id };
        postdata[field] = value;
        $.ajax({
          url: apiurl + "robot/info/set",
          type: 'POST',
          dataType: 'json',
          data: JSON.stringify(postdata),
          headers: {
            'Content-Type': 'application/json'
          },
          success: function (r) {
            if (r.status == 0) {
              layer.msg('修改成功');
            } else {
              layer.msg(r.msg, { icon: 5, time: 1000 });
            }
          },
          error: function (xhr, status, error) {
            layer.msg(error);
          }
        });

      });


      form.on('submit(search)', function (data) {
        let postdata = data.field;
        postdata.sid = sid;
        table.reload('list_table', {
          url: apiurl + 'WorkingCondition'
          , contentType: 'application/json'
          , where: postdata
          //, width : 1000
          , page: {
            layout: ['count', 'prev', 'page', 'next', 'refresh']
            , curr: 1
          }
        });

        return false;
      });

    });


  </script>
</body>

</html>